﻿using FastReport;
using FastReport.Export.Image;
using FastReport.Utils;
using FastReport.Web;
using JYH.Business;
using JYH.Framework;
using JYH.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

namespace JYH.Web.MES.Public.UI
{
    public partial class ReportPrint : System.Web.UI.Page
    {

        Report report { get; set; } = new Report();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               var PrintName= Request["PrintName"];
               var QueryName = Request["QueryName"];
                Type type = Type.GetType("JYH.Web.MES.Public.UI.ReportPrint");
                if (type != null)
                {
                    object instance = Activator.CreateInstance(type);
                    MethodInfo method = type.GetMethod(PrintName, BindingFlags.Public | BindingFlags.Instance);
                    if (method != null)
                    {
                        object[] args=new object[1];
                        args[0] = QueryName;
                  
                    object result = method.Invoke(instance, args);
                    }
                }

               
            }

         }

        protected void WebReport1_StartReport(object sender, EventArgs e)
        {
            WebReport1.Report = report;
        }
        public bool PrintMaterialBarcode(string IDs)
        {
            // 查找客户的报告
            //Reports reportItem = reportItems.FirstOrDefault((p) => p.Id == id);
            string Msg = "";
            BasBarcodeInfoBLL bll = new BasBarcodeInfoBLL();
            //SalPartnerLableBLL lableBLL = new SalPartnerLableBLL();
            //MfgWorkOrderBarcodeBLL barcodeBLL = new MfgWorkOrderBarcodeBLL();
            string printQty ="1";
            dynamic info = JsonConvert.DeserializeObject(IDs);
            string Ids = info.CodeID;
            SysParaInfo paraInfo = new SysParaInfo();
            paraInfo = SysParaInfoBLL.GetParaInfo("MaterialBarcode", out Msg);
            string BarcodeLable = "MaterialBarcode";
            if (paraInfo != null)
            {
                BarcodeLable = paraInfo.ParaValue;
            }

            DataSet dataSet = new DataSet();
            // 获取数据源
            dataSet = bll.GetWmsBarCodeInfo(Ids, printQty, out Msg);
            if (dataSet != null && dataSet.Tables[0] != null)
            {
                dataSet.Tables[0].TableName = "BasBarcodeInfo";
            }
            // 文件地址
            string reportPath = HostingEnvironment.MapPath("~/App_Data/Lable/" + BarcodeLable + ".frx");
            // 创建内存流以存储报告数据
            MemoryStream stream = new MemoryStream();

            try
            {
                // 启用FastReport的Web模式
                //Config.WebMode = true;

            
                    // 加载报告
                    report.Load(reportPath);
                    //注入到报告
                   // report.RegisterData(dataSet);
                    report.Dictionary.Connections[0].ConnectionString = ConfigHelper.GetConnectionString("DBCONN");
                    report.RegisterData(dataSet.Tables[0], dataSet.Tables[0].TableName);

                  report.Refresh();
                    //启动当前数据源
                    //report.Report.GetDataSource(dataSet.Tables[0].TableName).Enabled = true;
                  //report.Report.Prepare(); // 确保数据准备完成
                   
                    // 根据查询参数导出报告
                   
             
                return true;
            }
            catch
            {
                return false;
                // 捕获异常并返回内部服务器错误
               
            }
        }

        protected void WebReport1_Load(object sender, EventArgs e)
        {

        }
    }
}